package com.share.lottery.base;

import com.share.lottery.R;
import com.share.lottery.view.CustomToast;

import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 基类
 * @author zph
 *
 */
public class BaseFragmentActivity extends FragmentActivity implements OnClickListener{
	private ProgressDialog pDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		IntentFilter exitFilter = new IntentFilter();
		exitFilter.addAction("finish");
		registerReceiver(mFinishReceiver, exitFilter);
	}
	/**
	 * 销毁当前界面
	 */
//	public void onFinish() {
//		this.finish();
//		overridePendingTransition(R.anim.in_from_right,R.anim.out_to_right);
//	}
	/**
	 * 跳转界面
	 * @param classz 跳转的界面
	 * @param isFinish 是否销毁当前界面 true:销毁
	 */
	public void turnToActivity(Class<?> classz, boolean isFinish) {
		startActivity(new Intent(this, classz));
		 
		if (isFinish) {
			this.finish();
		}
		
		overridePendingTransition(R.anim.in_from_right,R.anim.out_to_right);
		
	}
	/**
	 * 销毁当前界面 直接跳转
	 * @param classz
	 */
	public void turnToActivity(Class<?> classz) {
		turnToActivity(classz, true);
	}
	public void showProgreessDialog() {

		if (pDialog == null) {
			pDialog = new ProgressDialog(this);
		}
		pDialog.setMessage("数据加载中..");
		pDialog.show();
	}
	/**
	 * 显示等待对话框 当点击返回键取消对话框并停留在该界面
	 * @param hint 要显示的文字
	 */
	public void showProgreessDialog(String hint) {
		if (pDialog == null) {
			pDialog = new ProgressDialog(this);
		}
		pDialog.setMessage(hint);
		pDialog.show();
		pDialog.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
				if(keyCode == KeyEvent.KEYCODE_BACK){
					try{
							dismissProgressDialog();
					}catch (Exception e) {
						e.printStackTrace();
					}
				}
				return false;
			}
		});
	}
	/**
	 * 销毁对话框
	 */
	public void dismissProgressDialog() {
		if (pDialog != null) {
			pDialog.dismiss();
		}
	}
	/**
	 * 弹一个短的土司
	 * @param str
	 */
	public void showShortToast(String str){
		CustomToast.makeText(this, str, 0);
	}
	/**
	 * 弹一个长的土司
	 * @param str
	 */
	public void showLongToast(String str){
		CustomToast.makeText(this, str,1);
	}
	@Override
	public void onClick(View v) {
		
	}
	
	
	public BroadcastReceiver mFinishReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			if ("finish".equals(intent.getAction())) {
				finish();
			}
		}
	};

	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(mFinishReceiver);

	};
	@Override
	protected void onResume() {
		super.onResume();
		// 页面开始
//		StatService.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 页面结束
//		StatService.onPause(this);
	}
	
	@Override
	public void finish() {
		// TODO Auto-generated method stub
		super.finish();
		overridePendingTransition(R.anim.in_from_right,R.anim.out_to_right);
	}
}
